﻿using System;
using System.Collections.Generic;
using System.Text;
using SdmxMl.Common;
using SdmxMl.Manager;
using SdmxMl.Structure;
using SdmxMl.GenericMetadata;

namespace SdmxMl.OrgGroup
{
    public class OrgSchemeGenerator
    {
        #region Internal Members

        private SmManager sm;
        private MetadataSetType metaSet;
        private MetadataStructureDefinitionType msd;
        private ReportStructureType report;

        #endregion

        #region C'tor

        /// <summary>C'tor with specified artefact to transform</summary>
        /// <param name="mSet">The generic metadataset</param>
        /// <param name="mdef"> the MSD describing metaset format</param>
        /// <param name="sm">Container with copy of memory content already loaded</param>
        public OrgSchemeGenerator(MetadataSetType mSet, MetadataStructureDefinitionType mdef, SmManager smMgr)
        {
            metaSet = mSet;
            msd = mdef;
            sm  = smMgr; ;

            //check null param
            if (msd == null)
                throw new ArgumentNullException("MSD");
            if (metaSet == null)
                throw new ArgumentNullException("MetadataSet");
            if (sm == null)
                throw new ArgumentNullException("SM Container");

            // check correct msd is refered
            if (metaSet.MetadataStructureRef.FullIdent != msd.FullIdent)
                throw new ArgumentException("Incorrect metadata structure received for metadataset");

            report = msd.ReportStructureList.LocateItem(metaSet.ReportRef);
            if (report == null)
                throw new ArgumentException("Incorrect report reference [" + metaSet.ReportRef + "], it does not exist in associated MSD");

            // Must be expected metadata for Organisation generation
            if (metaSet.Id != "GMD_SMS_UGRP")
                throw new ApplicationException("Invalid metadataset ID");

        }

        #endregion

    }
}
